package com.okilo.online.app.tools;

import android.util.TypedValue;

import com.okilo.online.app.OkiloApplication;

/**
 * DensityUtil
 * @Author wwdeng
 * @CreateTime: 2024/11/27 11:18
 * @Annotation：单位转换类
 */
public class OkiloDensityUtil {

    private OkiloDensityUtil() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * dp转px
     *
     * @return
     */
    public static int dp2px(float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, OkiloApplication.app.getResources().getDisplayMetrics());
    }

    /**
     * sp转px
     *
     * @return
     */
    public static int sp2px(float spVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                spVal, OkiloApplication.app.getResources().getDisplayMetrics());
    }

    /**
     * px转dp
     *
     * @param pxVal
     * @return
     */
    public static float px2dp(float pxVal) {
        final float scale = OkiloApplication.app.getResources().getDisplayMetrics().density;
        return (pxVal / scale);
    }

    /**
     * px转sp
     *
     * @param pxVal
     * @return
     */
    public static float px2sp(float pxVal) {
        return (pxVal / OkiloApplication.app.getResources().getDisplayMetrics().scaledDensity);
    }
}
